﻿//
// Copyright (c) 2011 Western Data Solutions (www.westerndatasolutions.com) (Armen Shimoon)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without 
// limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 
// the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.IO;
using System.Security.AccessControl;
using WDS.Bencode.Parsing;

namespace WDS.Bencode
{
    /// <summary>
    /// Extensions for interacting with the bencoding library
    /// </summary>
    public static class Extensions
    {
        /// <summary>
        /// Parses the data stream into a generic root bencode element
        /// </summary>
        /// <param name="st">Stream containing bencoded data</param>
        /// <returns>Generic bencode element</returns>
        public static BencodeElement ToBencodeElement(this Stream st)
        {
            return new StreamParser(st).ParseAs<BencodeElement>();
        }

        /// <summary>
        /// Parses the data stream into a dictionary bencode element
        /// </summary>
        /// <param name="st">Stream containing bencoded data</param>
        /// <returns>Bencode dictionary element</returns>
        public static BencodeDictionary ToBencodeDictionaryElement(this Stream st)
        {
            return new StreamParser(st).ParseAs<BencodeDictionary>();
        }

        /// <summary>
        /// Parses the data stream into a integer bencode element
        /// </summary>
        /// <param name="st">Stream containing bencoded data</param>
        /// <returns>Bencode integer element</returns>
        public static BencodeInteger ToBencodeIntegerElement(this Stream st)
        {
            return new StreamParser(st).ParseAs<BencodeInteger>();
        }

        /// <summary>
        /// Parses the data stream to a list bencode element
        /// </summary>
        /// <param name="st">Stream containing bencoded data</param>
        /// <returns>Bencoded list element</returns>
        public static BencodeList ToBencodeListElement(this Stream st)
        {
            return new StreamParser(st).ParseAs<BencodeList>();
        }

        /// <summary>
        /// Parses the data stream to a string bencode element
        /// </summary>
        /// <param name="st">Stream containing bencoded data</param>
        /// <returns>Bencoded string element</returns>
        public static BencodeString ToBencodeStringElement(this Stream st)
        {
            return new StreamParser(st).ParseAs<BencodeString>();
        }

        /// <summary>
        /// Parses the string data into a generic bencode element
        /// </summary>
        /// <param name="data">Bencoded data</param>
        /// <returns>Generic bencode element</returns>
        public static BencodeElement ToBencodeElement(this string data)
        {
            return new StreamParser(new MemoryStream(System.Text.ASCIIEncoding.Default.GetBytes(data))).ParseAs<BencodeElement>();
        }

        /// <summary>
        /// Parses the string data into a dictionary bencode element
        /// </summary>
        /// <param name="data">Bencoded data</param>
        /// <returns>Dictionary bencode element</returns>
        public static BencodeDictionary ToBencodeDictionaryElement(this string data)
        {
            return new StreamParser(new MemoryStream(System.Text.ASCIIEncoding.Default.GetBytes(data))).ParseAs<BencodeDictionary>();
        }

        /// <summary>
        /// Parses the string data into a list bencode element
        /// </summary>
        /// <param name="data">Bencoded data</param>
        /// <returns>List bencode element</returns>
        public static BencodeList ToBencodeListElement(this string data)
        {
            return new StreamParser(new MemoryStream(System.Text.ASCIIEncoding.Default.GetBytes(data))).ParseAs<BencodeList>();
        }

        /// <summary>
        /// Parses the string data into a string bencode element
        /// </summary>
        /// <param name="data">Bencoded data</param>
        /// <returns>String bencode element</returns>
        public static BencodeString ToBencodeStringElement(this string data)
        {
            return new StreamParser(new MemoryStream(System.Text.ASCIIEncoding.Default.GetBytes(data))).ParseAs<BencodeString>();
        }

        /// <summary>
        /// Parses the string data into an integer bencode element
        /// </summary>
        /// <param name="data">Bencoded data</param>
        /// <returns>Integer bencode element</returns>
        public static BencodeInteger ToBencodeIntegerElement(this string data)
        {
            return new StreamParser(
                new MemoryStream
                    (System.Text.ASCIIEncoding.Default.GetBytes(data))).ParseAs<BencodeInteger>();
        }

        /// <summary>
        /// Outputs the bencode element to a bencoded string
        /// </summary>
        /// <param name="element">The element to encode</param>
        /// <returns>A bencoded string representing the bencode element</returns>
        public static string ToBencodedString(this BencodeElement element)
        {
            return System.Text.ASCIIEncoding.Default.GetString(element.ToMemoryStream().ToArray());
        }

        /// <summary>
        /// Outputs the bencode element to a bencoded string sitting in a memory stream
        /// </summary>
        /// <param name="element">Bencode element to encode</param>
        /// <returns>A memory stream containing the bencoded string representation of the bencode element</returns>
        public static MemoryStream ToMemoryStream(this BencodeElement element)
        {
            var str = new MemoryStream();
            element.WriteToStream(str);

            return str;
        }

        /// <summary>
        /// Encodes the bencode element to the given file path
        /// </summary>
        /// <param name="element">The bencode element to encode</param>
        /// <param name="path">The path to write the file to</param>
        public static void WriteToFile(this BencodeElement element, string path)
        {
            element.WriteToStream(new System.IO.FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite));
        }

        /// <summary>
        /// Encodes the bencode element to the given stream
        /// </summary>
        /// <param name="element">Bencode element to encode</param>
        /// <param name="str">The output stream to write to</param>
        public static void WriteToStream(this BencodeElement element, Stream str)
        {
            new StreamBencodeGenerator(element).Encode(str);
        }


    }
}
